<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/7/6
     * Time: 下午3:31
     */

    namespace app\payment\logic;

    use app\payment\model\PaymentOrderModel;
    use app\payment\model\WalletModel;
    use Omnipay\Omnipay;
    use think\Db;

    class OmniPayAliPay extends PayBase
    {

        static public function init()
        {

            $config = module_config('payment');

            /**
             * @var AopAppGateway $gateway
             */
            $gateway = Omnipay::create('Alipay_AopApp');
            $gateway->setSignType('RSA2'); //RSA/RSA2
            $gateway->setAppId($config['alipay_app_id']);
            $gateway->setPrivateKey($config['app_private_key']);
            $gateway->setAlipayPublicKey($config['alipay_public_key']);
            $gateway->setNotifyUrl(request()->domain() . self::Alipay_Notify_Url);

            return $gateway;
        }

        static public function send($out_trade_no,$total_fee)
        {

            $gateway = static::init();

            $response = $gateway->purchase()->setBizContent([
                'subject'      => self::Body . ':' . ($total_fee / 100) . '元',
                'out_trade_no' => $out_trade_no,
                'total_amount' => $total_fee / 100,
                'product_code' => 'FAST_INSTANT_TRADE_PAY',
            ])->send();

            return $response->getOrderString();
        }

        /**
         *
         */
        static public function callback()
        {

            $gateway = static::init();
            $request = $gateway->completePurchase();
            $request->setParams($_POST);
            /**
             * @var AopCompletePurchaseResponse $response
             */
            try
            {
                $response = $request->send();

                if ($response->isPaid())
                {
                    try
                    {
                        Db::startTrans();
                        $data['order_sn'] = $_POST['out_trade_no'];
                        $data['pay_type'] = 'alipay';
                        $data['trade_no'] = $_POST['trade_no'];
                        $data['time_end'] = date('Y-m-d H:i:s',time());

                        $PaymentOrderModel = PaymentOrderModel::where(['pay_sn' => $_POST['out_trade_no']])->find();

                        $PaymentOrderModel->trade_sn = $_POST['trade_no'];
                        $PaymentOrderModel->status   = 1;
                        $PaymentOrderModel->save();

                        WalletModel::where(['member_id' => $PaymentOrderModel->member_id])->setInc('money',$PaymentOrderModel->money);
                        Db::commit();

                    }
                    catch (\Exception $exception)
                    {
                        Db::rollback();

                    }

                    /**
                     * Payment is successful
                     */
                    die('success');
                }
                else
                {
                    /**
                     * Payment is not successful
                     */
                    die('fail');
                }
            }
            catch (\Exception $e)
            {
                /**
                 * Payment is not successful
                 */
                die('fail');
            }
        }

    }